﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace webmonitor
{
    public partial class Form1 : Form
    {
        string content_start = "";
        string content_now = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tb_url.Text = tb_url.Text.Trim();
            System.Net.HttpWebRequest request =
                    (System.Net.HttpWebRequest)System.Net.WebRequest.Create(
                         new Uri( tb_url.Text));
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            string content_start = sr.ReadToEnd();

            textBox1.Text = content_start;
            tb_time.Text =tb_time .Text .Trim();
            lb_count.Text = tb_time.Text;
            timer1.Interval = Convert.ToInt16(tb_time.Text) * 1000;
            timer1.Enabled = true;
            timer1s.Enabled = true;
            listBox1.Items.Add(DateTime .Now .ToString ()+"监视开始！");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tb_url.Text = tb_url.Text.Trim();
            System.Net.HttpWebRequest request =
                    (System.Net.HttpWebRequest)System.Net.WebRequest.Create(
                         new Uri(tb_url.Text));
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            content_now = sr.ReadToEnd();

            textBox2.Text = content_now;

            if (textBox1.Text == textBox2.Text)
            {
                lb_count.Text = tb_time.Text;
                listBox1.Items.Add(DateTime.Now.ToString() + "内容无变化！");
            }
            else {
                timer1.Enabled = false;
                timer1s.Enabled = false;
                listBox1.Items.Add(DateTime.Now.ToString() + "内容变化！");
                System.Diagnostics.Process.Start(tb_url.Text );
                string[] cont_old = content_start.Split('\r');
                string[] cont_new = content_now.Split('\r');
                for (int i=0;i<cont_old.Length;i++)
                {
                    if (cont_old[i] != cont_new[i])
                    { 
                        lb_alert.Text = "变化出现在第" + (i+1).ToString() + "行";
                    break;
                    }
                        

                }
            
            }
            listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];


        }

        private void timer1s_Tick(object sender, EventArgs e)
        {
            lb_count.Text=(Convert.ToInt16(lb_count.Text) - 1).ToString();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter("webmonitor.txt");
            sw.WriteLine(tb_url.Text);
            sw.WriteLine(tb_time.Text);
            sw.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                System.IO.StreamReader sr = new System.IO.StreamReader("webmonitor.txt");
                tb_url.Text = sr.ReadLine();
                tb_time.Text = sr.ReadLine();
                sr.Close();
            }
            catch { }
        }
    }
}
